using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;
using Server.Targeting;
using Server.HuePickers;


namespace Server.Gumps
{
	public class ManagerSetupGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 107;
		private static readonly int ValueWidth = 128;
		private static readonly int EntryCount = 8;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private VendorManager m_Manager;
		private int m_btOffset = 20000;

		public ManagerSetupGump( VendorManager vm ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_Manager = vm;
			string name = "";
			string sval = "";

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + TotalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, TotalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, HeaderTextHue, "Manager Setup" );

			for ( int i = 0; i < EntryCount; i++ )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "Set area";
						if ( vm.VendorArea != null && vm.VendorArea.Length > 0 )
							sval = "Ready";

						break;
					}
					case 1: { name = "Add Tiles"; sval = ""; break; }
					case 2: { name = "Spawn Wanderer"; sval = vm.GetMallWorkers( typeof( WanderingTrader ) ).ToString(); break; }
					case 3: { name = "Spawn Restocker"; sval = vm.GetMallWorkers( typeof( RestockableVendor ) ).ToString(); break; }
					case 4: { name = "Spawn Petseller"; sval = vm.GetMallWorkers( typeof( PetSeller) ).ToString(); break; }
					case 5: { name = "All go home"; sval = ""; break; }
					case 6: { name = "Setup Tile"; sval = String.Format( "{0} (0x{0:X})", vm.TileID ); break; }
					case 7: { name = "Convert Tiles"; sval = ""; break; }
//					default: name = "Test features"; sval = "Object Properties"; break;
				}

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, name );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset + 1, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			if ( m_Manager == null || m_Manager.Deleted )
				return;

			Mobile from = state.Mobile;
			VendorManager vm = m_Manager;
			int adjusted = info.ButtonID - m_btOffset;

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1:
				{
					if ( vm.VendorArea == null || !( vm.VendorArea.Length > 0 ) )
						vm.BeginMarkArea( from, vm );
					else
					{
						from.CloseGump( typeof( MallSetupGump ) );
						from.SendGump( new MallSetupGump( vm ) );
					}

					break;
				}
				case 2:
				{
					VendorManager.AddVendorTile( from, (Mobile) vm );

					break;
				}
				case 3:
				{
					VendorManager.ToSpawnCommVendor( from, vm, typeof( WanderingTrader ) );

					break;
				}
				case 4:
				{
					VendorManager.ToSpawnCommVendor( from, vm, typeof( RestockableVendor ) );

					break;
				}
				case 5:
				{
					VendorManager.ToSpawnCommVendor( from, vm, typeof( PetSeller ) );

					break;
				}
				case 6:
				{
					VendorManager.AllGoHome( from, vm );

					break;
				}
				case 7:
				{
					VendorManager.ToSetupTile( from, vm );

					break;
				}
				case 8:
				{
					VendorManager.ToConvertTiles( from, vm );

					break;
				}
			}
		}

	}

	public class MallSetupGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 107;
		private static readonly int ValueWidth = 128;
		private static readonly int EntryCount = 2;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private VendorManager m_Manager;
		private int m_btOffset = 20000;

		public MallSetupGump( VendorManager vm ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_Manager = vm;
			string name = "";
			string sval = "";

			int entryCount = EntryCount;

			if ( vm.VendorArea != null && vm.VendorArea.Length > 0 )
				entryCount += vm.VendorArea.Length;

			int totalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (entryCount + 1));


			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, HeaderTextHue, "Manager Setup" );

			for ( int i = 0; i < entryCount; i++ )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "Add an area";
						if ( vm.VendorArea != null && vm.VendorArea.Length > 0 )
							sval = "Ready";

						break;
					}
					case 1: { name = "Clear all areas"; sval = ""; break; }
					default:
					{
						name = "Area " + ( i - 1 ).ToString();

						Rectangle3D rect = vm.VendorArea[i - 2];

						string content = string.Format( "( {0}, {1}, {2} )", rect.Start.X, rect.Start.Y, rect.Start.Z )
							+ String.Format( " - ( {0}, {1}, {2} )", rect.End.X, rect.End.Y, rect.End.Z );

						sval = content;

						break;
					}
				}

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, name );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset + 1, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			if ( m_Manager == null || m_Manager.Deleted )
				return;

			Mobile from = state.Mobile;
			VendorManager vm = m_Manager;
			int adjusted = info.ButtonID - m_btOffset;

//			from.SendMessage( "button: {0}.", adjusted );		// debug code

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1:
				{
					vm.BeginMarkArea( from, vm );

					break;
				}
				case 2:
				{
					VendorManager.ToClearArea( from, vm, -1 );

					break;
				}
				default:
				{
					if ( adjusted > 2 )
						VendorManager.ToClearArea( from, vm, adjusted - ( 2 + 1 ) );

					break;
				}
				
			}

		}
	}

	public class TileSetupGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = 46;	// PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 107;
		private static readonly int ValueWidth = 128;
		private static readonly int EntryCount = 2;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ( (EntryHeight + OffsetSize) * (EntryCount) + 20 );

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private VendorManager m_Manager;
		private int m_btOffset = 20000;

		public TileSetupGump( VendorManager vm ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_Manager = vm;
			string name = "";
			string sval = "";

			Item tile = new CommVendorTile( vm.TileID, vm.TileHue );

			int totalHeight = TotalHeight;
			int entryHeight = EntryHeight;

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, 20, HeaderTextHue, "Tile Setup" );

			for ( int i = 0; i < EntryCount; i++ )
			{
				x = BorderSize + OffsetSize;

				if ( i == 0 )
					y += 20 + OffsetSize;
				else
					y += entryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "";
						sval = String.Format( "{0} (0x{0:X})", tile.ItemID );

						break;
					}
					case 1:
					{
						name = "Hue Picker";
						sval = String.Format( "{0} (0x{0:X})", tile.Hue );

						break;
					}
				}

				AddImageTiled( x, y, NameWidth, entryHeight, EntryGumpID );

				if ( i == 0 )
					AddItem( x + TextOffsetX, y, tile.ItemID, tile.Hue );
				else
					AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, entryHeight, TextHue, name );

				x += NameWidth + OffsetSize;
					AddImageTiled( x, y, ValueWidth, entryHeight, EntryGumpID );

				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, entryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, entryHeight, SetGumpID );

				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset + 1, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			if ( m_Manager == null || m_Manager.Deleted )
				return;

			Mobile from = state.Mobile;
			VendorManager vm = m_Manager;
			int adjusted = info.ButtonID - m_btOffset;

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1:
				{
					ArrayList buttons = TileDisplayGump.CreateTileItems( from );

					if( buttons.Count > 0 )
					{
						from.CloseGump( typeof( TileDisplayGump ) );
						from.SendGump( new TileDisplayGump( vm, buttons ) );
					}

					break;
				}
				case 2:
				{
					from.SendHuePicker( new InternalPicker( vm.TileID, from, vm ) );

					break;
				}
			}
		}

		private class InternalPicker : HuePicker
		{
			private Mobile m_From;
			private VendorManager m_Manager;

			public InternalPicker( int itemID, Mobile from, VendorManager manager ) : base( itemID )
			{
				m_From = from;
				m_Manager = manager as VendorManager;
			}

			public override void OnResponse( int hue )
			{
				m_Manager.TileHue = hue;

				m_From.CloseGump( typeof( TileSetupGump ) );
				m_From.SendGump( new TileSetupGump( m_Manager ) );
			}
		}

	}
}